
/****************************************************************************
  * @file     SD93F115B_adc.h   
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
*********************************************************************************/

#ifndef __SD93F115B_SDADC_H
#define __SD93F115B_SDADC_H

#ifdef __cplusplus
 extern "C" {
#endif

/********************* Includes**************************/
#include "SD93F115B.h"


/***************************ADC Init structure definition********************/

typedef enum
{
	IA_RCF_0=0x00,
	IA_RCF_20k=0x01,
	IA_RCF_160k=0x02,
	IA_RCF_320k=0x03	
}IARCF_TypeDef;

#define IS_SDADC_IARCF(RCF) (((RCF)==IA_RCF_0 )||((RCF)==IA_RCF_20k)||\
							 ((RCF)==IA_RCF_160k)||((RCF)==IA_RCF_320k))
typedef enum
{
	IA_GAIN1=0x00,
	IA_GAIN4=0x01,
	IA_GAIN8=0x02,
	IA_GAIN16=0x03,
	IA_GAIN32=0x04,
	IA_GAIN64=0x05,
	IA_GAIN128=0x06,
	IA_GAIN256=0x07
	
}IAGAIN_TypeDef;

typedef enum
{
	VOS_Negative_Phase=0x00,
	VOS_Positive_Phase=0x01	
}IAOSSIGN_TypeDef;

typedef enum
{
	IA_IAD_x1=0x00,
	IA_IAD_x2=0x01,
	IA_IAD_x7_2=0x10,
	IA_IAD_x5=0x11
	
}IAD_TypeDef;

#define IS_SDADC_IAGAIN(GAIN) (((GAIN)==IA_GAIN1)||((GAIN)==IA_GAIN4)||\
                               ((GAIN)==IA_GAIN8)||((GAIN)==IA_GAIN16)||\
                               ((GAIN)==IA_GAIN32)||((GAIN)==IA_GAIN64)||\
							   ((GAIN)==IA_GAIN128)||((GAIN)==IA_GAIN256))
#define IS_SDADC_VOS(VOS)            (((VOS) > =0) && ((VOS) <= 0x06))		
#define IS_SDADC_OSSIGN(OSSIGN)      (((OSSIGN)==VOS_Negative_Phase)||((OSSIGN)==VOS_Positive_Phase))
#define IS_SDADC_IAD(IAD)		     (((IAD)==IA_IAD_x1)||((IAD)==IA_IAD_x2)||\
									((IAD)==IA_IAD_x7_2)||((IAD)==IA_IAD_x5))	   
typedef enum
{
	IA_ExteralChopper_OFF=0x00,
	IA_ExteralChopper_DIV256=0x01,
	IA_ExteralChopper_DIV512=0x02,
	IA_ExteralChopper_DIV1024=0x03,
	IA_ExteralChopper_DIV2048=0x04,
	IA_ExteralChopper_DIV4096=0x05,
	IA_ExteralChopper_DIV8192=0x06,
	IA_ExteralChopper_OFF_0x07=0x07
	
}ExteralChopper_TypeDef;

#define IS_SDADC_IA_ExteralChopper(ExChopper) (((ExChopper)==IA_ExteralChopper_DIV256)||((ExChopper)==IA_ExteralChopper_DIV512)||\
                               ((ExChopper)==IA_ExteralChopper_DIV1024)||((ExChopper)==IA_ExteralChopper_DIV2048)||\
                               ((ExChopper)==IA_ExteralChopper_DIV4096)||((ExChopper)==IA_ExteralChopper_DIV8192)||\
							   ((ExChopper)==IA_ExteralChopper_OFF)||((ExChopper)==IA_ExteralChopper_OFF_0x07))	
typedef enum
{
	IA_InteralChopper_OFF=0x00,
	IA_InteralChopper_DIV16=0x01,
	IA_InteralChopper_DIV32=0x02,
	IA_InteralChopper_DIV64=0x03,
	IA_InteralChopper_DIV128=0x04,
	IA_InteralChopper_DIV256=0x05,
	IA_InteralChopper_DIV512=0x06,
	IA_InteralChopper_OFF_0x07=0x07
	
}InteralChopper_TypeDef;

#define IS_SDADC_IA_InteralChopper(Chopper) (((Chopper)==IA_InteralChopper_DIV16)||((Chopper)==IA_InteralChopper_DIV32)||\
                               ((Chopper)==IA_InteralChopper_DIV64)||((Chopper)==IA_InteralChopper_DIV128)||\
                               ((Chopper)==IA_InteralChopper_DIV256)||((Chopper)==IA_InteralChopper_DIV512)||\
							   ((Chopper)==IA_InteralChopper_OFF)||((Chopper)==IA_InteralChopper_OFF_0x07))	

typedef enum
{
	IA_SIA_HIGH=0x00,
	IA_SIA_LOW=0x01	
	
}SIA_TypeDef;

#define IS_SDADC_SIA(SIA)		     (((SIA)==IA_SIA_LOW)||((PIN)==IA_SIA_HIGH))
						   
/*********************ADC_Input_Singal*******************************/
#define SDADC_IAINPS_A0					    ((uint8_t)0x00)		
#define SDADC_IAINPS_A1            		    ((uint8_t)0x01)
#define SDADC_IAINPS_A2             		((uint8_t)0x02)
#define SDADC_IAINPS_A3            		    ((uint8_t)0x03)
#define SDADC_IAINPS_A4            		    ((uint8_t)0x04)
#define SDADC_IAINPS_A5             		((uint8_t)0x05)
#define SDADC_IAINPS_A6           			((uint8_t)0x06)
#define SDADC_IAINPS_A7            		    ((uint8_t)0x07)
#define SDADC_IAINPS_VSS             		((uint8_t)0x08)
#define SDADC_IAINPS_ACM            		((uint8_t)0x09)
#define SDADC_IAINPS_OPBO            		((uint8_t)0x0A)
#define SDADC_IAINPS_OPAO            		((uint8_t)0x0B)
#define SDADC_IAINPS_DAO            		((uint8_t)0x0C)
#define SDADC_IAINPS_A12            		((uint8_t)0x0D)
#define SDADC_IAINPS_TSP             		((uint8_t)0x0E)
#define SDADC_IAINPS_TSN            		((uint8_t)0x0F)

#define SDADC_IAINNS_A0					    SDADC_IAINPS_A0		
#define SDADC_IAINNS_A1            		    SDADC_IAINPS_A1
#define SDADC_IAINNS_A2             		SDADC_IAINPS_A2
#define SDADC_IAINNS_A3            		    SDADC_IAINPS_A3
#define SDADC_IAINNS_A4            		    SDADC_IAINPS_A4
#define SDADC_IAINNS_A5             		SDADC_IAINPS_A5
#define SDADC_IAINNS_A6           			SDADC_IAINPS_A6
#define SDADC_IAINNS_A7            		    SDADC_IAINPS_A7
#define SDADC_IAINNS_VSS             		SDADC_IAINPS_VSS
#define SDADC_IAINNS_ACM            		SDADC_IAINPS_ACM
#define SDADC_IAINNS_OPBO            		SDADC_IAINPS_OPBO
#define SDADC_IAINNS_OPAO              	    SDADC_IAINPS_OPAO 
#define SDADC_IAINNS_DAO            		SDADC_IAINPS_DAO
#define SDADC_IAINNS_A12            		SDADC_IAINPS_A12
#define SDADC_IAINNS_TSP             		SDADC_IAINPS_TSP
#define SDADC_IAINNS_TSN            		SDADC_IAINPS_TSN

#define IS_SDADC_IAINPS_Channel(Channel) (((Channel)==SDADC_IAINPS_A0)||((Channel)==SDADC_IAINPS_A1)||\
								   ((Channel)==SDADC_IAINPS_A2)||((Channel)==SDADC_IAINPS_A3)||\
								   ((Channel)==SDADC_IAINPS_A4)||((Channel)==SDADC_IAINPS_A5)||\
								   ((Channel)==SDADC_IAINPS_A6)||((Channel)==SDADC_IAINPS_A7)||\
								   ((Channel)==SDADC_IAINPS_VSS)||((Channel)==SDADC_IAINPS_ACM)||\
								   ((Channel)==SDADC_IAINPS_OPBO)||((Channel)==SDADC_IAINPS_OPAO)||\
								   ((Channel)==SDADC_IAINPS_DAO)||((Channel)==SDADC_IAINPS_A12)||\
								   ((Channel)==SDADC_IAINPS_TSP)||((Channel)==SDADC_IAINPS_TSN))	
						   
#define IS_SDADC_IAINNS_Channel(Channel) (((Channel)==SDADC_IAINNS_A0)||((Channel)==SDADC_IAINNS_A1)||\
								   ((Channel)==SDADC_IAINNS_A2)||((Channel)==SDADC_IAINNS_A3)||\
								   ((Channel)==SDADC_IAINNS_A4)||((Channel)==SDADC_IAINNS_A5)||\
								   ((Channel)==SDADC_IAINNS_A6)||((Channel)==SDADC_IAINNS_A7)||\
								   ((Channel)==SDADC_IAINNS_VSS)||((Channel)==SDADC_IAINNS_ACM)||\
								   ((Channel)==SDADC_IAINNS_OPBO)||((Channel)==SDADC_IAINNS_OPAO)||\
								   ((Channel)==SDADC_IAINNS_DAO)||((Channel)==SDADC_IAINNS_A12)||\
								   ((Channel)==SDADC_IAINNS_TSP)||((Channel)==SDADC_IAINNS_TSN))							   
							   							   
typedef struct
{
  uint8_t IA_ExteralChopper;       //  外部chopper（fl）频率设置
  uint8_t IA_InteralChopper;       //  内部chopper（fh）频率设置
  FunctionalState IA_OSEN;         //  PGIA修调位使能
  uint8_t IA_VOS;                  //  PGIA修调位设置
  IAOSSIGN_TypeDef IA_OSSIGN;       //  PGIA修调正负号选择
  IAD_TypeDef IA_IAD;              //  PGIA偏置电流选择控制位
  uint8_t IA_RCF;                  //  设置RC滤波器中R的大小
  uint8_t IA_GAIN;                 //  PGIA增益选择
  FunctionalState IA_SIEN;         //  IAINP、IAINN短接控制位
  uint8_t IA_SIA;                  //  PGIA输入端面积选择控制位
  uint8_t SDADC_IAINNS;            //  输入信号负端选择
  uint8_t SDADC_IAINPS;            //  输入信号正端选择
    
}IA_InitTypeDef;


typedef enum
{
	SDADC_BUFFDSOFF=0x00,
	SDADC_BUFFDS8=0x01,
	SDADC_BUFFDS16=0x02,
	SDADC_BUFFDS32=0x03,
	SDADC_BUFFDS64=0x04,
	SDADC_BUFFDS128=0x05,
	SDADC_BUFFDS256=0x06,	
	SDADC_BUFFDSOFF_07=0x07,
}SDADCBUFCHOP_TypeDef;

#define IS_SDADC_BUFCHOP(CHOP) (((CHOP)==SDADC_BUFFDS8)||((CHOP)==SDADC_BUFFDS16)||\
                                ((CHOP)==SDADC_BUFFDS32)||((CHOP)==SDADC_BUFFDS64)||\
							    ((CHOP)==SDADC_BUFFDS128)||((CHOP)==SDADC_BUFFDS256)||\
								((CHOP)==SDADC_BUFFDSOFF)||((CHOP)==SDADC_BUFFDSOFF_07))
								
	
typedef enum
{
	SDADC_DCSET0=0x00,
	SDADC_DCSETP1_4=0x01,
	SDADC_DCSETP1_2=0x02,
	SDADC_DCSETP3_4=0x03,
	SDADC_DCSETN1_4=0x05,
	SDADC_DCSETN1_2=0x06,	
	SDADC_DCSETN3_4=0x07,
    SDADC_DCSET0_04=0x04
}SDADCDCSET_TypeDef;

#define IS_SDADC_DCSET(DCSET) (((DCSET)==SDADC_DCSETP1_4)||((DCSET)==SDADC_DCSETP1_2)||\
							   ((DCSET)==SDADC_DCSETP3_4)||((DCSET)==SDADC_DCSETN1_4)||\
							   ((DCSET)==SDADC_DCSETN1_2)||((DCSET)==SDADC_DCSETN3_4)||\
							   ((DCSET)==SDADC_DCSET0)||(DCSET)==SDADC_DCSET0_04))

typedef enum
{
	SDADC_VREFG1=0x00,
	SDADC_VREFG1_2=0x01,
	SDADC_VREFG1_4=0x02,
	SDADC_VREFG1_8=0x03	
}SDADCVREFG_TypeDef;

#define IS_SDADC_VREFG(VREFG) (((VREFG)==SDADC_VREFG1)||((VREFG)==SDADC_VREFG1_2)||\
							   ((VREFG)==SDADC_VREFG1_4)||((VREFG)==SDADC_VREFG1_8))

typedef enum
{
	SDADC_VREFS_AVDDRVSS=0x00,
	SDADC_VREFS_ACMVSS=0x01,
	SDADC_VREFS_A2A3=0x02,
	SDADC_VREFS_AVDDRACM=0x03	
}SDADCVREFS_TypeDef;

typedef enum
{
	SDADC_SINC2=0x00,
	SDADC_SINC3=0x01
}SDADCSINC_TypeDef;

#define IS_SDADC_VREFS(VREFS) (((VREFS)==SDADC_VREFS_AVDDRVSS)||((VREFS)==SDADC_VREFS_ACMVSS)||\
							   ((VREFS)==SDADC_VREFS_A2A3)||((VREFS)==SDADC_VREFS_AVDDRACM))
#define IS_SDADC_ASPCK1(ASPCK1) (((ASPCK1) > =0) && ((ASPCK1) <= 0xff))
#define IS_SDADC_ADOUTS(ADOUTS) (((ADOUTS)==SDADC_SINC2)||((ADOUTS)==SDADC_SINC3))
typedef enum
{
	SDADC_OSR128=0x00,
	SDADC_OSR256=0x01,
	SDADC_OSR512=0x02,
	SDADC_OSR1024=0x03,
	SDADC_OSR2048=0x04,
	SDADC_OSR4096=0x05,
	SDADC_OSR8192=0x06,
	SDADC_OSR16384=0x07		
	
}SDADCOSR_TypeDef;
#define IS_SDADC_OSR(OSR) (((OSR)==SDADC_OSR128)||((OSR)==SDADC_OSR256)||\
						   ((OSR)==SDADC_OSR512)||((OSR)==SDADC_OSR1024)||\
						   ((OSR)==SDADC_OSR2048)||((OSR)==SDADC_OSR4096)||\
						   ((OSR)==SDADC_OSR8192)||((OSR)==SDADC_OSR16384))						   
typedef struct
{
  uint8_t SDADC_BUFFDS;                 // Buf1/Buf2 chopper频率fb设置位;设置时需要保证fb>fh>fl
  FunctionalState SDADC_Buf1;           // Buf1使能位
  FunctionalState SDADC_Buf2;           // Buf2使能位
  uint8_t SDADC_DCSET;                  // ADC失调电压选择
  uint8_t SDADC_VREFG;                  // ADC参考电压放大倍数选择
  uint8_t SDADC_VREFS;                  // ADC参考电压源选择
  uint32_t SDADC_ASPCK1;                // 用来设定ADC的采样频率fs
  SDADCSINC_TypeDef SDADC_ADOUTS;         // SINC2/SINC3滤波器选择位
  uint8_t SDADC_OSR;                    // 数字滤波器过采样率选择位
  FunctionalState SDADC_DIEN;           // ADC dither使能位
  FunctionalState SDADC_PAEN;           // ADC chopper控制位，仅在数字滤波器选择SINC2时有效
   
}SDADC_InitTypeDef;


/***************************EMI****************************************/
typedef struct
{
  FunctionalState EMI_DELEN;            //选择EMI检测结果是否再进行延时滤波
  uint8_t EMI_VTH;                      //设置EMI的检测阈值
  
}EMI_InitTypeDef;

#define EMIA							((uint8_t)0x00)	
#define EMIB							((uint8_t)0x01) 
#define IS_EMI_VTH(VTH)              (((VTH) > =0) && ((VTH) <= 0x03))
/**********************IBOUT***************************/										
#define SDADC_IBOUT_0uA					((uint32_t)0x00000000)	
#define SDADC_IBOUT_0uA5                IA_CR_IBOUT_0
#define SDADC_IBOUT_2uA                 IA_CR_IBOUT_1
#define SDADC_IBOUT_4uA                 IA_CR_IBOUT
#define IS_SDADC_IBOUT(IBOUT) (((IBOUT)==SDADC_IBOUT_0uA )||((IBOUT)==SDADC_IBOUT_0uA5)||\
							   ((IBOUT)==SDADC_IBOUT_2uA)||((IBOUT)==SDADC_IBOUT_4uA))

/***************************Temperature Sensor****************************************/
typedef struct
{     
	FunctionalState TempSensor_TVBS;                			//设置温度传感器输出buffer的不同尾电流
 
	FunctionalState TempSensor_TCPEN;							//使能温度传感器正反测功能
	
	FunctionalState TempSensor_TSEL;        				    //选择输出不同的温度传感器信号 
	
    FunctionalState TempSensor_TSOEN;                           //使能温度传感器的输出           
    
}TempSensor_InitTypeDef;

 
/* Initialization and Configuration functions *********************************/ 

void SDADC_DeInit(void);               //SDADC相关寄存器恢复为上电默认值

void SDADC_IAInit(IA_InitTypeDef* SDADC_IAInitStruct);				      //SDADC IA初始化
void SDADC_PGIACmd(FunctionalState NewState);						      //PGIA使能

void SDADC_ChannelConfig(uint8_t SDADC_IAINPS,uint8_t SDADC_IAINNS);      //配置SDADC信号输入通道 
void SDADC_EMISCmd(FunctionalState NewState);                             //PGIA抗EMI干扰使能

void SDADC_Init(SDADC_InitTypeDef* SDADC_InitStruct);				      //SDADC初始化
void SDADC_Cmd(FunctionalState NewState);                                 //使能SDADC

void SDADC_Buf1Cmd(FunctionalState NewState);						      //BUFFER1使能
void SDADC_Buf2Cmd(FunctionalState NewState);						      //BUFFER2使能
void SDADC_P2ENCmd(FunctionalState NewState);			                  //ADC内部时钟测试

/******************Interrupts and flags management functions **********************************/
void SDADC_ITCmd(FunctionalState NewState);                               //使能SDADC中断
ITStatus SDADC_GetITStatus(void);                                         //获取SDADC中断标志位
void SDADC_ClearITStatus(void);                                           //清除SDADC中断标志位

uint32_t SDADC_GetConversionValue(void);						          //获取SDADC转换结果

/*************************IBOUT*********************************/
void SDADC_IBOUTCofig(uint32_t SDADC_IBOUT);                              //设置开短探测模块的输出电流

/**************************EMI**********************************/
void SDADC_EMIInit(uint8_t EMIx,EMI_InitTypeDef* EMI_InitStruct);         //初始化EMI检测模块
void SDADC_EMICmd(uint8_t EMIx,FunctionalState NewState);                 //使能EMI检测模块
FlagStatus SDADC_GetEMIFlagStatus(uint8_t EMIx);                          //获取EMI检测标志位
void SDADC_ClearEMIFlagStatus(uint8_t EMIx);                              //清除EMI检测标志位

/**************************TempMeasure functions**********************************/
void SDADC_TempSensorInit(TempSensor_InitTypeDef* TempSensor_InitStruct);      //初始化温度传感器的相关配置  注：软件测温时，还需对ADC进行初始化，
void SDADC_SoftTempMeasurerCmd(FunctionalState NewState);				       //实现温度信号的正反测，ENABLE,正测；DISABLE,反测

void SDADC_HardTempMeasureCmd(FunctionalState NewState);		              //使能硬件测温功能


#ifdef __cplusplus
}
#endif

#endif /* __SD93F115_ADC_H */

/**********************************END OF FILE*********************************/
